package com.lp.biz;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lp.biz.identity.mapper.BizIdentityMapper;
import com.lp.biz.identity.mapper.BizIdentityMemberRelMapper;
import com.lp.biz.identity.model.entity.BizIdentity;
import com.lp.biz.identity.model.entity.BizIdentityMemberRel;
import com.lp.common.component.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static com.lp.common.constant.CacheConstant.USER_DETAIL_KEY;
import static com.lp.common.constant.CommonConstant.NEGATE;
import static com.lp.common.constant.CommonConstant.SURE;
import static com.lp.common.constant.IdentityConstant.*;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-11-23 22:58:04
 */
@Component
public class ComponentService {

    @Autowired
    private RedisService redis;

    @Autowired
    private BizIdentityMapper identityMapper;

    @Autowired
    private BizIdentityMemberRelMapper identityMemberRelMapper;

    /**
     * 增加新身份
     *
     * @param newRel      新身份关系
     * @param newIdentity 新身份
     */
    public void setDefault(BizIdentityMemberRel newRel, BizIdentity newIdentity) {
        // 设置新关系为有效和默认
        newRel.setIsEffective(SURE);
        newRel.setIsDefault(NEGATE);
        redis.delete(USER_DETAIL_KEY + newRel.getMemberId());

        // 查询当前成员已有的有效身份关系
        for (BizIdentityMemberRel rel : identityMemberRelMapper.selectList(new QueryWrapper<BizIdentityMemberRel>()
                .lambda()
                .eq(BizIdentityMemberRel::getMemberId, newRel.getMemberId())
                .eq(BizIdentityMemberRel::getIsEffective, SURE)
                .orderByAsc(BizIdentityMemberRel::getCreateAt))) {
            // 根据身份id查询对应的身份信息
            BizIdentity exitsIdentity = identityMapper.selectById(rel.getBizIdentityId());
            if (exitsIdentity != null && ObjectUtil.notEqual(TYPE_SENIOR, exitsIdentity.getTypeCode())
                    && ObjectUtil.notEqual(TYPE_PROMOTION, exitsIdentity.getTypeCode())
                    && ObjectUtil.notEqual(TYPE_SENIOR, newIdentity.getTypeCode())
                    && ObjectUtil.notEqual(TYPE_PROMOTION, newIdentity.getTypeCode())) {
                if (rel.getBizIdentityId().equals(newRel.getBizIdentityId())) {
                    newRel.setIsEffective(rel.getIsEffective());
                    newRel.setIsDefault(rel.getIsDefault());
                    break;
                }
                if (exitsIdentity.getCode().equals(CLASS_TEACHER)) {
                    newRel.setIsDefault(NEGATE);
                    newRel.setIsEffective(NEGATE);
                    identityMemberRelMapper.update(null, new UpdateWrapper<BizIdentityMemberRel>()
                            .lambda()
                            .eq(BizIdentityMemberRel::getMemberId, rel.getMemberId())
                            .eq(BizIdentityMemberRel::getBizIdentityId, rel.getBizIdentityId())
                            .set(BizIdentityMemberRel::getIsDefault, SURE)
                            .set(BizIdentityMemberRel::getIsEffective, SURE));
                    break;
                } else if (newIdentity.getCode().equals(CLASS_TEACHER)) {
                    identityMemberRelMapper.update(null, new UpdateWrapper<BizIdentityMemberRel>()
                            .lambda()
                            .eq(BizIdentityMemberRel::getMemberId, rel.getMemberId())
                            .eq(BizIdentityMemberRel::getBizIdentityId, rel.getBizIdentityId())
                            .set(BizIdentityMemberRel::getIsDefault, NEGATE)
                            .set(BizIdentityMemberRel::getIsEffective, NEGATE));
                    break;
                } else {
                    // 根据逗号分隔的preIds，判断是否包含现有身份的id
                    Set<String> strings = new HashSet<>(Arrays.asList(newIdentity.getPreIds().split(",")));
                    if (!strings.contains(String.valueOf(exitsIdentity.getId()))) {
                        // 如果不包含，将新身份设为无效和非默认
                        newRel.setIsEffective(NEGATE);
                        newRel.setIsDefault(NEGATE);
                        break;
                    } else {
                        identityMemberRelMapper.update(null, new UpdateWrapper<BizIdentityMemberRel>()
                                .lambda()
                                .eq(BizIdentityMemberRel::getMemberId, rel.getMemberId())
                                .eq(BizIdentityMemberRel::getBizIdentityId, rel.getBizIdentityId())
                                .set(BizIdentityMemberRel::getIsDefault, NEGATE)
                                .set(BizIdentityMemberRel::getIsEffective, NEGATE));
                    }
                }
            }
        }
        identityMemberRelMapper.update(null, new UpdateWrapper<BizIdentityMemberRel>().lambda()
                .eq(BizIdentityMemberRel::getMemberId, newRel.getMemberId())
                .eq(BizIdentityMemberRel::getBizIdentityId, newRel.getBizIdentityId())
                .set(BizIdentityMemberRel::getIsDefault, newRel.getIsDefault())
                .set(BizIdentityMemberRel::getIsEffective, newRel.getIsEffective())
                .set(BizIdentityMemberRel::getCreateAt, newRel.getCreateAt())
                .set(BizIdentityMemberRel::getSource, newRel.getSource()));
        redis.delete(USER_DETAIL_KEY + newRel.getMemberId());
    }

}
